<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class StrategyService
{
    // 主域名
    const MASTER_DOMAIN = 'https://api2.100credit.cn';
    // 沙箱域名
    const SANDBOX_DOMAIN = 'https://sandbox-api2.100credit.cn';

    // 贷前策略API
    const STRATEGY_URI = '/strategy_api/v3/hx_query';
    // 信息验证API
    const INFO_VERIFY_URI = '/infoverify/v3/info_verify';
    //报告下载API
    const REPORT_DOWNLOAD_URI = '/query_api/v3/download_approval_report';

    private $apiCode;

    private $appKey;

    private $sandbox;

    private $strategy_id;

    private $conf_id;

    private $action;

    public function __construct()
    {
        $config = config('strategy');

        $this->apiCode = $config['apiCode'];
        $this->appKey = $config['appKey'];
        $this->sandbox = $config['sandbox'];
        $this->strategy_id = $config['strategy_id'];
        $this->conf_id = $config['conf_id'];
    }

    /**
     * Create a instance.
     * @return $this
     */
    public static function make()
    {
        return new static();
    }

    /**
     * @param $action
     * @return $this
     */
    public function setAction($action)
    {
        $this->action = $action;

        return $this;
    }

    /**
     * 组装jsonData参数
     * @param array $params
     * @return false|string
     */
    private function buildJsonData(array $params)
    {
        $data = [];

        if ($this->action == 'strategy') {
            $data['strategy_id'] = $this->strategy_id;
            $data['id'] = $params['id'];
            $data['cell'] = $params['cell'];
            $data['name'] = $params['name'];
        }

        if ($this->action == 'verification') {
            $data['conf_id'] = $this->conf_id;
            $data['id'] = $params['id'];
            $data['cell'] = $params['cell'];
            $data['name'] = $params['name'];
        }

        if ($this->action == 'report_download') {
            $data['swift_number'] = $params['swift_number'];
        }

        return json_encode($data);
    }

    /**
     * 加密请求数据
     * @param array $params
     * @return string
     */
    private function encryptJsonData(array $params): string
    {
        $jsonData = $this->buildJsonData($params);

        return AesService::encrypt(urlencode($jsonData), $this->appKey);
    }

    /**
     * 生成校验码
     * @param $params
     * @return string
     */
    private function generateCheckCode($params): string
    {
        $jsonData = $this->buildJsonData($params);
        $apiCode = $this->apiCode;
        $appKey = $this->appKey;

        return md5($jsonData . $apiCode . $appKey);
    }

    public function query($params)
    {
        $domain = $this->sandbox ? static::SANDBOX_DOMAIN : static::MASTER_DOMAIN;

        $uri = '';
        if ($this->action == 'strategy') {
            $uri = static::STRATEGY_URI;
        } elseif ($this->action == 'verification') {
            $uri = static::INFO_VERIFY_URI;
        } elseif ($this->action == 'report_download') {
            $uri = static::REPORT_DOWNLOAD_URI;
        }

        $url = $domain . $uri;
        $response = Http::asForm()->withoutVerifying()->post($url, [
            'appKey' => md5($this->appKey),
            'apiCode' => $this->apiCode,
            'jsonData' => $this->encryptJsonData($params),
            'checkCode' => $this->generateCheckCode($params),
        ]);

        return $response->body();
    }
}
